﻿package com.urbansquall.ginger.tools
{
	import flash.display.*;
	import flash.geom.Rectangle;
	
	public class AnimationPlayerBuilder
	{
		/**
		 * Reads and converts an XML file to an AnimationPlayer 
		 * @param a_xml The XML segment defining the AnimationPlayer
		 * @param a_bitmapDataLibrary The BitmapDataLibrary that holds the path to any images referenced in the XML
		 * @return The newly constructed AnimationPlayer
		 * 
		 */		
		public static function parse( a_xml : XML, a_bitmapDataLibrary : IBitmapDataLibrary ) : AnimationPlayer
		{
			var player : AnimationPlayer = new AnimationPlayer();
				
			var fps : int = parseInt( a_xml.@fps );
			var defaultAnimation : String = a_xml.@defaultAnimation;
			var rotations : int = 1;
			if( a_xml.@rotations != "" )
			{
				rotations = parseInt( a_xml.@rotations );
			}
			
			for each( var animationXML : XML in a_xml.animation )
			{
				var id : String = animationXML.@id;
				var bitmap : String = animationXML.@bitmap;
				var looping : Boolean = false;
				var animationFPS : int = fps;
				if( animationXML.@fps != "" )
				{
					animationFPS = parseInt( animationXML.@fps );
				}
				
				if( animationXML.@isLooping != "" )
				{
					var loopingStringValue : String = animationXML.@isLooping;
					if( loopingStringValue.toLowerCase().charAt( 0 ) == "t" || loopingStringValue.toLowerCase().charAt( 0 ) == "1" || loopingStringValue.toLowerCase().charAt( 0 ) == "y" )
					{
						looping = true;
					}
				}
				var bd : BitmapData = a_bitmapDataLibrary.lookupBitmapData( bitmap );

				var startX : int = 0;
				var startY : int = 0;
				
				var animation : Animation;
				
				if( animationXML.tilesheet.length() > 0 )
				{
					var tilesheet : XML = animationXML.tilesheet[ 0 ];
					var tileWidth : int = parseInt( tilesheet.@tileWidth );
					var tileHeight : int = parseInt( tilesheet.@tileHeight );
					
					// bitmapAnimation = AnimationBuilder.importStrip( animationFPS, bd, tileWidth, tileHeight, -1, 0, 0, rotations );
					
					animation = new Animation( animationFPS, rotations );
					
					var rows : int = Math.ceil( bd.height / tileHeight );
					var cols : int = Math.ceil( bd.width / tileWidth );

					var frameLocation : Rectangle;
					
					var tileCount : int = rows * cols;
					
					var keyframeIndexes : Array = [];
					var keyframeIDs : Array = [];
					
					for each( var keyframeXML : XML in tilesheet.keyframe )
					{
						var frameIndex : int = parseInt( keyframeXML.@frameIndex );
						var keyframeID : String = keyframeXML.@id;
						
						keyframeIndexes.push( frameIndex );
						keyframeIDs.push( keyframeID );
					}
										
					for( var i: int = 0; i < tileCount; i++ )
					{
						var tileX : int = (i % cols);
						var tileY : int = Math.floor( i / cols );
						
						var frameID : String = "";
						if( keyframeIndexes.indexOf( i ) >= 0 )
						{
							frameID = keyframeIDs[ keyframeIndexes.indexOf( i ) ];
						}
						
						frameLocation = new Rectangle(
							tileX * tileWidth, tileY * tileHeight,
							tileWidth, tileHeight );
						animation.addFrame( 1, bd, frameLocation, tileWidth/2, tileHeight/2, frameID );
					}
				}
				else
				{
					animation = new Animation( animationFPS, rotations );
					
					for each( var frameXML : XML in animationXML.frame )
					{
						var offsetX : Number = frameXML.@offsetX;
						var offsetY : Number = frameXML.@offsetY;
						var frameWidth : Number = frameXML.@width;
						var frameHeight : Number = frameXML.@height;
						keyframeID = frameXML.@keyframeID;
						
						if( startX + frameWidth > bd.width )
						{
							startX = 0;
							startY += frameHeight;
						}
						
						animation.addFrame( 1, bd, new Rectangle( startX, startY, frameWidth, frameHeight ), offsetX, offsetY, keyframeID );
						
						// trace( "start", startX, startY, "size", frameWidth, frameHeight, "offset", offsetX, offsetY, keyframeID );
						
						startX += frameWidth;						
					}
				}
				
				if( looping )
				{
					animation.isLooping = true;
				}
				
				player.addAnimation( id, animation );													
			}
			
			player.defaultAnimationID = defaultAnimation;
			player.play( defaultAnimation );
						
			return player;
		}
	}
	
}